<?php

namespace app\home\controller;

use app\common\controller\QyApi;
use app\company\service\DataService;
use library\Controller;
use think\Db;

use function Qiniu\json_decode;

/**
 * 企业微信员工列表管理
 * Class User
 * @package app\worker\controller
 */
class QyUser extends Controller
{
    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'qy_user';

    /**
     * 企业微信员工列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '员工列表';
        $this->_query($this->table)
        ->like('name')
        ->page();
    }

    protected function _index_page_filter(&$data) {
        //获取部门名称
        // $qyApi = new QyApi();
        foreach($data as &$v){
            $v['department'] = [];
        //     $department = json_decode($v['department'], true);
        //     $department_name = [];
        //     foreach($department as $vo){
        //         $department_name[] = $qyApi->department_list($vo)['department'][0]['name'] ?? "";
        //     }
        // $v['department'] = $department_name;
            
        }
    }

    /**
     * 同步员工列表信息
     * @auth true
     */
    public function get_user()
    {
        $qyApi = new QyApi();
        //获取部门列表
        $res = $qyApi->user_list();
        $user_list = $res['userlist'];
        $data = [];
        foreach($user_list as $v){
            $data[] = [
                'userid' => $v['userid'],
                'name' => $v['name'],
                'avatar' => $v['avatar'],
                'mobile' => $v['mobile'],
                'position' => $v['position'],
                'email' => $v['email'],
                'department' => json_encode($v['department']),
                'gender' => $v['gender'],
                'status' => $v['status'],
                'status' => $v['status'],
            ];
        }
        Db::name($this->table)->where('1=1')->delete();
        Db::name($this->table)->insertAll($data);
        $this->success('同步成功');
    }

    public function getcheckindata()
    {
        $this->title = '考勤信息';
        $userid = request()->get('userid');
        if(empty($userid)){
            $this->error('缺少参数userid');
        }
        $starttime = date("Y-m-d",strtotime("-30 day")) . ' 00:00:00';
        $endtime = date('Y-m-d') . ' 00:00:00';
        $data = [
            'opencheckindatatype' => 3,
            'starttime' => strtotime($starttime),
            'endtime' => strtotime($endtime),
            'useridlist' => [$userid],
        ];
        $qyApi = new QyApi();
        $res = $qyApi->getcheckindata($data);
        if ($res['errcode'] == 0) {
            $this->list = $res['checkindata'];
        } else {
            $this->error($res['errmsg']);
        }
        $this->fetch();
    }

    //发送消息
    public function message_send()
    {
        $data = [
            'touser' => 'CaiGuLiangDeXiaoMoGu',
            'msgtype' => "text",
            'agentid' => 3010011,
            'text' => [
                'content' => "发送成功"
            ],
        ];
        $qyApi = new QyApi();
        $res = $qyApi->message_send($data);
        halt($res);
    }
}
